package com.warmlight.voicepacket.service;

import android.app.IntentService;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;

import com.umeng.analytics.MobclickAgent;
import com.warmlight.voicepacket.utils.ClickLog;
import com.warmlight.voicepacket.utils.DownloadHelper;
import com.warmlight.voicepacket.utils.FileUtils;
import com.warmlight.voicepacket.utils.ToastUtil;

import java.io.File;

public class ADService extends IntentService {

    public ADService() {
        super(null);
    }

    private Handler handler;

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */
    public ADService(String name) {
        super(name);
    }

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        handler = new Handler();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        String apk = intent.getStringExtra("apk");
        downloadAPK(apk);
    }

    private void downloadAPK(final String download_url){
        String name = FileUtils.getFileNameUrl(download_url);
        DownloadHelper.getInstance().download(download_url, name + ".apk", new DownloadHelper.OnDownloadListener() {
            @Override
            public void onDownloadSuccess(final File file) {
                try {
                    MobclickAgent.onEvent(getApplicationContext(),"dis_banner_down",download_url);
                    ClickLog.clickLog("dis_banner_down","0",download_url);
                    anzhuang(file);
                }catch (Exception e){

                }
            }

            @Override
            public void onDownloading(final int progress) {

            }

            @Override
            public void onDownloadFailed(Exception e) {
                if (handler != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtil.showShort(ADService.this, "哎呀，下载失败了");
                        }
                    });
                }
            }
        });
    }

    private void anzhuang(File file){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(this, "com.warmlight.voicepacket.FileProvider", file);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        startActivity(intent);
        //强制更新就把应用关掉
//        android.os.Process.killProcess(android.os.Process.myPid());
    }


}
